﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasySL.Entity;

namespace EasySL.Data
{
    public class DAO
    {
        public List<Product> mockDB;

        public DAO()
        {
            this.mockDB = new List<Product>();

            for (int i = 0; i < 100; i++)
            {
                this.mockDB.Add(new Product()
                {
                    Id = i + 1,
                    Name = string.Format("product_{0}", i + 1)
                });
            }
        }

        public bool Connect()
        {
            return true;
        }

        public int GetProductCount()
        {
            return this.mockDB.Count;
        }

        public List<Product> GetProducts(int pageIndex, int pageSize)
        {
            return mockDB.OrderByDescending(p => p.Id).Skip(pageIndex * pageSize).Take(pageSize).ToList();
        }

        public void SaveOrAddProduct(Product product)
        {
            //int i = 0;
            //i = 10 % i;
            
            var prd = this.mockDB.SingleOrDefault(p => p.Id == product.Id);
            if (prd == null)
            {
                prd = new Product();
                prd.Id = this.mockDB.Last().Id + 1;
                prd.Name = product.Name;
                this.mockDB.Add(prd);
            }
            else
            {
                prd.Name = product.Name;
            }
        }

        public void DeleteProduct(int id)
        {
            var prd = this.mockDB.SingleOrDefault(p => p.Id == id);
            this.mockDB.Remove(prd);
        }
    }
}
